package top.huahaizhi.mpchart.demo;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;

import java.util.ArrayList;
import java.util.Random;

/**
 * xbo 饼状图
 */
public class PieChartActivity extends AppCompatActivity {

    private PieChart pieChart;
    private DrawThread drawThread;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pie_chart);
        pieChart = (PieChart) findViewById(R.id.piechart);
        drawThread = new DrawThread();
        drawPie();
    }

    public void drawPie() {
        //名字
        ArrayList<String> names = new ArrayList<String>();
        names.add("java");
        names.add("python");
        names.add("C++");
        names.add("C#");
        names.add("PHP");
        names.add("JavaScript");
        //大小
        ArrayList<Entry> sizes = new ArrayList<Entry>();
        Random random = new Random();
        for (int i = 0; i < names.size(); i++) {
            sizes.add(new Entry(random.nextInt(50), i));
        }

        //颜色
        ArrayList<Integer> colors = new ArrayList<Integer>();
        for (int i = 0; i < names.size(); i++) {
            colors.add(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
        }
        PieDataSet pieDataSet = new PieDataSet(sizes, "");//参数：颜色栏显示颜色目录
        pieDataSet.setValueTextSize(10f);
        //pieDataSet.setDrawValues(true);//是否在块上面显示值以及百分百
        //pieDataSet.setSliceSpace(0f);//块间距
        pieDataSet.setColors(colors);


        //DisplayMetrics metrics=this.getResources().getDisplayMetrics();
        PieData pieData = new PieData(names, pieDataSet);

        pieChart.setHoleRadius(120f);  //半径
        pieChart.setTransparentCircleRadius(50f); // 半透明圈
        //pieChart.setTransparentCircleRadius(0f);//设置大圆里面透明小圆半径，和洞不是一个圆

        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleColorTransparent(true);//设置中心洞是否透明：true为黑，false为白
        pieChart.setHoleRadius(50f);//设置大圆里面的无色圆的半径（洞...）
        pieChart.setCenterText("编程语言市场比例");  //饼状图中间的文字

        pieChart.setDescription("");//参数：右下角显示图形描述

        //pieChart.setDrawCenterText(false);//不显示图中心文字
        //pieChart.setCenterText("traffic graph");//图中心文字
        pieChart.setRotationEnabled(true);//手动旋转

        //pieChart.setDrawMarkerViews(false);
        //pieChart.setDrawSliceText(false);//块的文本是否显示

        pieChart.setData(pieData);

        Legend legend = pieChart.getLegend();
        legend.setEnabled(true);//是否显示图形说明，必须要放在setData后,否则出错
        legend.setTextSize(15f);

        //两个参数有不同的意思：
        //durationMillisX：每个块运行到固定初始位置的时间
        //durationMillisY: 每个块到绘制结束时间
        pieChart.animateXY(1000, 1000);//设置动画（参数为时间）

        // 开启死循环
        drawThread.getHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                drawPie();
            }
        }, 3000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (drawThread != null) {
            drawThread.getHandler().removeCallbacksAndMessages(null);
            drawThread.getHandler().getLooper().quit();
            drawThread.setHandler(null);
            drawThread = null;
        }
    }
}
